package com.bitmain.mmst.web.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bitmain.mmst.common.consts.ResultCode.ERROR;
import com.bitmain.mmst.common.consts.ResultCode.INFO;
import com.bitmain.mmst.core.struct.ServiceResult;
import com.bitmain.mmst.domain.MineHashrateOverview;
import com.bitmain.mmst.domain.ShelfHashrateOverview;
import com.bitmain.mmst.feature.time.TimeService;
import com.bitmain.mmst.web.dao.OverviewDao;
import com.bitmain.ulnar.aider.DateAider;

/**
 * 
 *
 * @author Richard - rui.wu@bitmain.com
 * @since 2016年8月16日 
 *
 */
@Service("overviewService")
public class OverviewService {

	@Autowired
	private OverviewDao overviewDao;
	
	public ServiceResult<MineHashrateOverview> fetchLatestHashrateOverview() {
		ServiceResult<MineHashrateOverview> result = new ServiceResult<MineHashrateOverview>();
		try {
			long detectTime = overviewDao.selectLatestHashrateOverviewDetectTime();
			MineHashrateOverview mineOverview = overviewDao.selectLatestMineHashrateOverview(detectTime);
			List<ShelfHashrateOverview> shelfOverviews = overviewDao.selectLatestShelfOverview(detectTime);
		
			if(mineOverview!=null){
				mineOverview.setShelfOverviews(shelfOverviews);
				mineOverview.setDetectTimeDisplay(DateAider.formatMillis(detectTime, TimeService.getTimeZone(), "yy-MM-dd HH:mm:ss"));
				mineOverview.setTimeZoneDisplay(TimeService.getMineTimeZone().getShortDisplay());
			}
			result.setData(mineOverview);
			result.setInfo(INFO.SUCCESS_QUERY);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError(ERROR.DB_SELECT);
		}
		return result;
	}
}
